adminPanel.controller("UserAdvanceGraphController",['cacheFactory','$http','$scope','$rootScope','$q','$filter',function(cacheFactory,$http,$scope,$rootScope,$q,$filter){
	var  thisObject=this;
	//advance filter variables
	$scope.totalSubjectLoop=[];
	$scope.totalGradeLoop=[];
	$scope.selectedSubjectList=[];
	$scope.selectedGrade=[];
	$scope.selectedTerm=[];
	$scope.chartType=1;
	$scope.periodModel=1;
	$scope.terms=[{key:1,value:"Term 1"},{key:2,value:"Term 2"},{key:3,value:"Term 3"},{key:4,value:"Term 4"}]
	$rootScope.searchCriteria.selectedPeriod=$scope.periodModel;
	
	this.setChartType=function(chartType){
		$scope.chartType=chartType;
	}
	this.setPeriodModel=function(periodModel){
		$scope.periodModel=periodModel;
	}
	$scope.notNull = function(p){
	    if (p != null){
	        return p;
	    } else{
	        return;
	    }
	};
	
	this.getAdvancedGraphData= function(){
		$http.post(BASE_URL+"user/handleRequest/USER_DASHBOARD_ADVANCED_GRAPH",$rootScope.searchCriteria).success(function(data){
			if(data.status=='SUCCESS'){
				//var data = google.visualization.arrayToDataTable(data.result[0].objects);
				var json = data.object;
				var data1= new google.visualization.DataTable(json);
				var options = {
						title: 'Overall Performance',
						hAxis: {title: 'Grade', titleTextStyle: {color: 'red'}},
						legend: {position: 'top', textStyle: {fontSize: 14}},
						chartArea: {width: '90%'}
				};
				var chart=null;
				// Instantiate and draw our chart, passing in some options.
				if($scope.chartType==1){
					chart = new google.visualization.ColumnChart(document.getElementById('advance_chart_div'));
					chart.draw(data1, options);
				}
				
				if($scope.chartType==2){
					chart = new google.visualization.LineChart(document.getElementById('advance_chart_div'));
					chart.draw(data1, options);
				}
				//$scope.showGrid=true;
			}else{
				//alert(data.status +":"+data.msg);
				//$scope.showGrid=true;
			}
		}).error(function(){
			ERROR_LOGGER("Server Error");
		});
	};
	this.showFilters=function(){
		$scope.totalSubjectLoop=[];
		for (var i=0; i<($scope.selectedStudentProfile.subjectList.length); i++){
			$scope.totalSubjectLoop.push($scope.selectedStudentProfile.subjectList[i]);
		}
		$scope.totalGradeLoop=[];
		for (var i=0; i<$scope.selectedStudentProfile.totalGradeInfo.length; i++){
			$scope.totalGradeLoop.push($scope.selectedStudentProfile.totalGradeInfo[i]);
		}
		thisObject.toggleFilters();
	};
	
	this.toggleFilters=function(){
		$('#filterModal').modal('toggle');
	};
	
	this.init=function(){
		thisObject.getAdvancedGraphData();
	}
	
	this.applyFliters=function(){
		$rootScope.searchCriteria.selectedPeriod=$scope.periodModel;
		$rootScope.searchCriteria.selectedSubjectList=$scope.selectedSubjectList;
		$rootScope.searchCriteria.selectedTerm=$scope.selectedTerm;
		$rootScope.searchCriteria.selectedGrade=$scope.selectedGrade;
		thisObject.getAdvancedGraphData();
		thisObject.toggleFilters();
	};
	
	//render all four quads on the page
	this.renderGraphs=function(data){
		if(data && data.e==='STUDENT_PROFILE_CHANGED'  && data.url.indexOf("student_advanced_graph")>-1){
			thisObject.getAdvancedGraphData();
		}
	};
	//attach handler to main screen select button
	RegisterEventHandler("UserAdvanceGraphController",thisObject.renderGraphs);
	
	
	this.toggleselectedSubjectList = function(subjectId) {
		var idx = $scope.selectedSubjectList.indexOf(subjectId);
		// is currently selected
		if (idx > -1) {
			$scope.selectedSubjectList.splice(idx, 1);
		}
		// is newly selected
		else {
			$scope.selectedSubjectList.push(subjectId);
		}
		
		$rootScope.searchCriteria.selectedSubjectList=$scope.selectedSubjectList;
	};
	
	this.toggleGradeSelection = function(year) {
		var idx = $scope.selectedGrade.indexOf(year);
		// is currently selected
		if (idx > -1) {
			$scope.selectedGrade.splice(idx, 1);
		}
		// is newly selected
		else {
			$scope.selectedGrade.push(year);
		}
		$rootScope.searchCriteria.selectedGrade=$scope.selectedGrade;
	};
	
	this.toggleTermSelection = function(termId) {
		var idx = $scope.selectedTerm.indexOf(termId);
		// is currently selected
		if (idx > -1) {
			$scope.selectedTerm.splice(idx, 1);
		}
		// is newly selected
		else {
			$scope.selectedTerm.push(termId);
		}
		$rootScope.searchCriteria.selectedTerm=$scope.selectedTerm;
	};
}]);